## creates tables for appendix F:
## detailed table for middle-top income comparison
## table A7

rm(list=ls())

library(tidyverse)
library(kableExtra)

# read in bootstrapped opinion data

opinion_bootstrapped <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners.RDS')

for (i in names(opinion_bootstrapped)) {
  assign(i, opinion_bootstrapped[[i]])
}


# set up matrices/vectors of polarization data
# to feed into plots/figures

dem_polarization_boot_50 <- abs(dem_top_boot - dem_middle_boot)
rep_polarization_boot_50 <- abs(rep_top_boot - rep_middle_boot)
all_polarization_boot_50 <- abs(all_top_boot - all_middle_boot)

# table A7 - middle vs. top income

polarization_tab_50 <- data.frame(expand.grid(party = c('rep', 'dem', 'all'),
                                              topic_6 = unique(issuetopics$topic_6),
                                              stringsAsFactors = FALSE),
                                  avgpol = NA,
                                  signif = NA,
                                  gilens = NA,
                                  gilenssignif = NA,
                                  disagree = NA,
                                  disagreesignif = NA,
                                  n_quest = NA,
                                  stringsAsFactors = FALSE)

for (i in 1:nrow(polarization_tab_50)) {
  temp.issues <- issuetopics$question[issuetopics$topic_6 == polarization_tab_50$topic_6[i]]
  temp.rich <- get(paste0(polarization_tab_50$party[i], '_top_boot'))
  temp.rich <- temp.rich[rownames(temp.rich) %in% temp.issues,]
  temp.mid <- get(paste0(polarization_tab_50$party[i], '_middle_boot'))
  temp.mid <- temp.mid[rownames(temp.mid) %in% temp.issues,]
  temp.pol <- get(paste0(polarization_tab_50$party[i], '_polarization_boot_50'))
  temp.pol <- temp.pol[rownames(temp.pol) %in% temp.issues,]
  
  polarization_tab_50$avgpol[i] <- median(colMeans(temp.pol))
  
  temp.ci <- apply(temp.rich - temp.mid, MARGIN = 1, function(x) quantile(x, c(0.025, 0.975)))
  polarization_tab_50$signif[i] <- sum(!apply(temp.ci, MARGIN = 2, function(x) between(0, x[1], x[2])))
  
  polarization_tab_50$gilens[i] <- sum(apply(temp.pol, MARGIN = 1, median) > 0.1)
  
  temp.ci <- apply(temp.pol, MARGIN = 1, function(x) quantile(x, c(0.025, 0.975)))
  polarization_tab_50$gilenssignif[i] <- sum(apply(temp.ci, MARGIN = 2, function(x) all(x > 0.1) | all(x < -0.1)))
  
  polarization_tab_50$disagree[i] <- sum(abs(apply((temp.rich > 0.5) - (temp.mid > 0.5), MARGIN =  1, median)))
  
  temp.ci <- apply((temp.rich > 0.5) - (temp.mid > 0.5), MARGIN = 1, function(x) quantile(x, c(0.025, 0.975)))
  polarization_tab_50$disagreesignif[i] <- sum(apply(temp.ci, MARGIN = 2, function(x) x[1]==x[2]& x[1]!=0))
  
  polarization_tab_50$n_quest[i] <- length(temp.issues)
}

for (p in unique(polarization_tab_50$party)) {
  i <- nrow(polarization_tab_50) + 1
  polarization_tab_50[i,] <- NA
  polarization_tab_50$topic_6[i] <- 'Total'
  polarization_tab_50$party[i] <- p
  
  temp.pol <- get(paste0(polarization_tab_50$party[i], '_polarization_boot_50'))
  
  polarization_tab_50$avgpol[i] <- median(colMeans(temp.pol))
  
  for (v in c('signif', 'gilens', 'gilenssignif', 'disagree', 'disagreesignif', 'n_quest')) { 
    polarization_tab_50[i,v] <- sum(polarization_tab_50[polarization_tab_50$party == p, v], na.rm = TRUE)
  }
}

polarization_tab_50 %>% 
  select(party, topic_6, n_quest, avgpol, signif, gilenssignif, disagreesignif) %>% 
  arrange(party) %>%
  mutate(party = case_when(party == 'rep' ~ 'Republicans',
                           party == 'dem' ~ 'Democrats',
                           party == 'all' ~ 'All Respondents'),
         topic_6 = case_when(topic_6 == 'foreignpolicy' ~ 'Foreign policy',
                             topic_6 == 'cultural' ~ 'Cultural',
                             topic_6 == 'immigration' ~ 'Immigration',
                             topic_6 == 'lawenforcement' ~ 'Law enforcement',
                             topic_6 == 'economic' ~ 'Economic',
                             topic_6 == 'socialwelfare' ~ 'Social welfare',
                             TRUE ~ topic_6)) %>% 
  mutate_at(vars(signif:disagreesignif), funs(. / n_quest)) %>% 
  mutate_if(is.numeric, round, 3) %>% 
  mutate_at(vars(signif:disagreesignif), funs(paste0(. * 100, '\\%'))) %>% 
  mutate(party = cell_spec(party, angle = 90, format = 'latex')) %>% 
  kable('latex', booktabs = TRUE, escape = FALSE, linesep = '\\addlinespace',
        col.names = linebreak(c(' ', 'Policy\nDomain', 'Number of\nUnique Issues', 'Average\nClass Gap', 'Opinion Difference\nStatistically\nSignificant', 
                                'Class Gap\nGreater than\n10 pts.', 'Class\nDisagreement\non Policy'),
                              align = 'c'),
        align = c('l', 'l', rep('c', ncol(.)-2))) %>%
  collapse_rows(1, latex_hline = 'major') %>% 
  row_spec(c(7, 14, 21), bold = TRUE) %>% 
  save_kable(file = 'appendix_tables/middle_detailed_polarization_summary.tex')
